package com.aios.action.password;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.aios.action.AbstractManagementAction;
import com.aios.entity.SystemUser;
import com.aios.service.PasswordChangeService;
import com.aios.service.impl.SystemUserServiceImpl;
import com.aios.util.EmailUtil;

public class PasswordChangeAction extends AbstractManagementAction {

	private static final long serialVersionUID = 1L;
	public static final byte active = 0;
	private static Logger logger = Logger.getLogger(PasswordChangeAction.class);

	private SystemUser systemUser;
	private String currentPassword;
	private String newPassword;
	private String confirmNewPassword;
	private SystemUser user;
	
	@Autowired 
	protected PasswordChangeService passwordChangeService;
	
	@Autowired
	private SystemUserServiceImpl systemUserServiceImpl;
	
	public String change() {
		return SUCCESS;
	}
	
	public String passwordChangeFrame(){
		return SUCCESS;
	}
	public String cancelBttnFrame(){
		return SUCCESS;
	}

	public String passwordChange() {
		try {

			validateEmail();
			if (hasErrors()) {
				return INPUT;
			}

			if (newPassword.equals(confirmNewPassword)) {
				user = systemUserServiceImpl.signIn(systemUser.getEmail(),systemUser.getUserPassword());

				if (user == null) {
					addActionError(getText("error.system.user.signin.failed"));
					return INPUT;
				} else {
					if(newPassword.equals(systemUser.getUserPassword())){
						addActionError(getText("error.same.password.again.failed"));
						return INPUT;
						
					}else{
					updatePasswordAndStatus(newPassword);
					return SUCCESS;
					}
				}
			}else{
				addActionError(getText("error.confirm.newPassword.failed"));
				return INPUT;
			}
	
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			return SUCCESS;
		}
	}

	private void validateEmail() {
		if (systemUser.getEmail().isEmpty()) {
			addFieldError("systemUser.email", getText("system.user.email.empty"));
		}
		
		if(!(EmailUtil.chackEmailPattern(systemUser.getEmail()))) {
			addActionError(getText("error.email.invalid"));
		}
	}
		
	private void updatePasswordAndStatus(String newPassword){
		
		try {
			user.setUserPassword(newPassword);
			user.setStatus(active);
			passwordChangeService.saveNewPasswordAndNewStatus(user,user.getId());
			passwordChangeService.savePasswordHistory(user,user.getId());
			
		} catch (Exception e) {
			logger.error(e);
		}
		
	}

	public String getCurrentPassword() {
		return currentPassword;
	}

	public void setCurrentPassword(String currentPassword) {
		this.currentPassword = currentPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmNewPassword() {
		return confirmNewPassword;
	}

	public void setConfirmNewPassword(String confirmNewPassword) {
		this.confirmNewPassword = confirmNewPassword;
	}

	public SystemUser getSystemUser() {
		return systemUser;
	}

	public void setSystemUser(SystemUser systemUser) {
		this.systemUser = systemUser;
	}

	public SystemUser getUser() {
		return user;
	}

	public void setUser(SystemUser user) {
		this.user = user;
	}
	
	
}

